package com.yueyi.yygh.hosp.util;

import com.yueyi.yygh.common.exception.YyghException;
import com.yueyi.yygh.common.helper.HttpRequestHelper;
import com.yueyi.yygh.common.result.ResultCodeEnum;
import com.yueyi.yygh.common.utils.MD5;
import com.yueyi.yygh.hosp.service.HospitalSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zyy
 * @create 2022-01-31 16:48
 */

public class signUtil {


    public static Map<String, Object> transObj(HttpServletRequest request){
        //获取传递过来的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        return paramMap;
    }

    public static Exception signMD5(Map<String, Object> paramMap,HospitalSetService hospitalSetService){

        //1 获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");
        //2 根据传递过来医院编码，查询数据库， 查询签名
        String  hoscode = (String)paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3 把数据库查询签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        // 4 判断签名是否一致
        if(!hospSign.equals(signKeyMD5)){
            return new YyghException(ResultCodeEnum.SIGN_ERROR);
        }else{
            return new YyghException(ResultCodeEnum.SIGN_SUCCESS);
        }

    }
}
